package hms.master;

import hms.database.DAO;
import hms.database.DAOnoGUI;
import hms.rooms.RoomTypeTable;
import hms.rooms.RoomType;

import java.sql.*;

/**
 * <h2>Class Overview</h2> 
 * The MasterController class is the controller that initializes the program and handles
 * running everything. It will create children to carry out individual tasks, but it is
 * basically the main class that handles the entire system.
 * 
 * @author Matthew Johnson
 */

// TODO: Add more detailed class overview

public class MasterController {

	private static DAO dbHandler;
	
	// TODO: Add comments for method
	public static void main(String[] args){
		
		// Create an instance of this class
		MasterController master = new MasterController();
		
		// Call the beginning method of this class which controls the program
		master.begin();

	}
	
	// TODO: Add comments for method
	@SuppressWarnings("unused")
	private void begin(){
		
		// Initialize DAO object
		dbHandler = new DAO("HMSDatabase","db_files\\", true);
		
		// Open DAO connection
		dbHandler.open();

		// Create the master view object
				MasterView master = null;

				
				

				// Create System Room and Room Type to hold unbooked guests
				if (initializeRoomRoomTypes()) {
					master = new MasterView();
				}
		
	}
	
	private boolean initializeRoomRoomTypes() {
		boolean flag = true;
		
		String ngRoomTestSQL = "Select count(*) as cnt from room where ROOMID=0";
		int ngRoomTestCnt = -1;
		ResultSet rs = null;
		DAO db = DAOnoGUI.getDAO();
		rs = db.getResult(ngRoomTestSQL);
		if (rs == null) {
			flag = false;
		} else {
			try {
				while(rs.next()){
					ngRoomTestCnt=rs.getInt("cnt");
				}
				if(ngRoomTestCnt < 1){
					String insertRoomSQL = "INSERT INTO ROOM(ROOMID, ROOMLABEL, ROOMTYPELABEL, FLOOR, ADDITIONALNOTES, OCCUPATIONSTATUS) " +
							"VALUES(?,?,?,?,?,?)";
					int roomid=0;
					String rmLbl ="System Only NO RESERVATION";
					String rmTypeLbl ="System Only";
					String addtlNotes ="System Only NO RESERVATION";
					int rmFlrNumber = 1;
					String ocupStatus = "System Only NO RESERVATION";

					PreparedStatement roomPS = db.generatePreparedStatement(insertRoomSQL);
					try {
						// manually insert room for unbooked guests
						roomPS.setInt(1, roomid);
						roomPS.setString(2,rmLbl);
						roomPS.setString(3,rmTypeLbl);
						roomPS.setInt(4, rmFlrNumber);
						roomPS.setString(5,addtlNotes);
						roomPS.setString(6, ocupStatus);
						db.executePreparedUpdate(roomPS);

						//System.out.println(roomPS);
						
					} catch (SQLException e) {
						e.printStackTrace();
						flag = false;
					}
					
				}
			} catch (SQLException e) {
				e.printStackTrace();
				flag = false;
			}
		}
		DAOnoGUI.closeDAO();

		return flag;
	}
	
	/**
	 * Get the global DAO object for manipulating and querying the database throughout
	 * the entire program
	 * 
	 * @return The static DAO object to be used everywhere
	 */
	public static DAO getDatabaseObject(){
		return dbHandler;
	}
	
	/**
	 * Closes the database connection
	 */
	public static void closeDatabaseConnection(){
		dbHandler.close();
	}
	
}

